﻿package com.kitchen
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class AbstractNpc extends MovieClip
	{
		public static var NPC_BACK:String = 'goback';
		public static var NPC_EAT:String = 'eat';
		public static var NPC_AHEAD:String = 'ahead';
		public static var NPC_ANGRY:String = 'angry';
		public static var NPC_STAND:String = 'stand';
		public static var NPC_LAUGH:String = 'laugh';
		public static var NPC_END:String = 'end';
		public static var NPC_WALK:String = 'walk';
		
		//npc data
		protected var npcData:Object;
		//npc 购物等待时间
		protected var wait:int = 0;
		//npc类型
		protected var id:int=0;
		//npc 状态
		protected  var step:int=0;
		//高度
		public var h:int;
		//宽度
		public var w:int;
		
		public function AbstractNpc(){
			super();
			this.addEventListener(Event.ENTER_FRAME, frameHandler);
			this.addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		//前奏
		public function goAhead():void{
			this.step = 1;
			this.wait = 0;
			this.gotoAndPlay(34);
			this.dispatchEvent(new Event(NPC_AHEAD));
		}
		
		//回走
		public function goBack():void{
			this.step = 6;
			this.wait = 0;
			this.gotoAndPlay(26);
			this.dispatchEvent(new Event(NPC_BACK));
		}
		
		//生气
		public function angry():void{
			this.step = 4;
			this.wait = 0;
			this.gotoAndPlay(2);
			this.dispatchEvent(new Event(NPC_ANGRY));
		}
		
		//笑嘻嘻
		public function laugh():void{
			this.step = 5;
			this.wait = 0;
			this.gotoAndPlay(10);
			this.dispatchEvent(new Event(NPC_LAUGH));
		}
		
		//站立柜台
		public function stand():void{
			this.step = 2;
			this.wait = 0;
			this.gotoAndPlay(18);
			this.dispatchEvent(new Event(NPC_STAND));
		}
		
		//吃
		public function eat(food:Object):void{
			this.step = 3;
			this.wait = 0;
			this.npcData.sMatch = food.sMatch;
			if(this.npcData.sFood == food.sMatch){
				this.laugh();
			}else{
				this.angry();
			}
			this.dispatchEvent(new Event(NPC_EAT));
		}
		
		//设置
		public function setConfig(cdata:Object):void{
			this.npcData = cdata;
		}
		
		//获取npc类型
		public function getId():int{
			return this.id;
		}
		
		//获取当前npc状态
		public function getStep():int{
			return this.step;
		}
		
		//获取npc数据
		public function getNpcData():Object{
			return this.npcData;
		}
		
		//npc 运动
		protected function frameHandler(event:Event):void{
			switch(this.step){
				case 0:
				break;
				//ahead
				case 1:
				if(this.x > this.npcData.tox){
					this.stand();
				}else{
					this.wait++;
					if(this.wait%10 == 0){
						this.dispatchEvent(new Event(NPC_WALK));
					}
					this.x += this.npcData.pace;
				}
				break;
				//stand
				case 2:
				if(this.wait > this.npcData.wait){
					this.angry();
				}else{
					this.wait++;
				}
				break;
				//eat
				case 3:
				if(this.wait > 10){
					this.laugh();
				}else{
					this.wait++;
				}
				break;
				//angry
				case 4:
				if(this.wait > 30){
					this.goBack();
				}else{
					this.wait++;
				}
				break;
				//laugh
				case 5:
				if(this.wait > 30){
					this.goBack();
				}else{
					this.wait++;
				}
				break;
				//back
				case 6:
				if(this.x < -100){
					this.step = 0;
					this.npcData = null;
					this.stop();
					this.dispatchEvent(new Event(NPC_END));
				}else{
					if(this.npcData.isBoss == 1){
						this.wait++;
						if(this.wait%10 == 0){
							this.dispatchEvent(new Event(NPC_WALK));
						}
					}
					this.x -= this.npcData.pace;
				}
				break;
			}
		}
		
		//送吃的
		protected function clickHandler(mevent:MouseEvent):void{
			if(this.step == 2){
				this.laugh();
			}
		}
	}
}